package com.haircut.project.service.finance.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haircut.project.constant.RecordConstant;
import com.haircut.project.dao.finance.FinanceDeptBatchMapper;
import com.haircut.project.dao.finance.FinanceDeptMapper;
import com.haircut.project.dao.finance.FinanceStaticMapper;
import com.haircut.project.model.finance.FinanceDept;
import com.haircut.project.model.finance.FinanceDeptBatch;
import com.haircut.project.service.finance.IFinanceDeptService;
import com.haircut.project.vo.finance.*;
import com.haircut.sys.utils.PageUtils;
import com.haircut.sys.utils.user.SecurityUtils;
import com.haircut.sys.vo.user.LoginUserVo;
import com.modular.constant.BaseCodeDesc;
import com.modular.exception.ModularException;
import com.modular.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FinanceDeptServiceImpl implements IFinanceDeptService {
    @Autowired
    private FinanceDeptMapper financeDeptMapper;
    @Autowired
    private FinanceDeptBatchMapper financeDeptBatchMapper;
    @Autowired
    private FinanceStaticMapper financeStaticMapper;
    /**
     * 和下游部门对账
     * @param reqVo
     * @return
     */
    @Override
    public PageResult<FinanceResVo> findSunDeptPage(FinanceReqVo reqVo)
    {
        LoginUserVo userVo = SecurityUtils.getLoginUser();
        reqVo.setParentId(userVo.getDeptId());
        IPage<?> pageReq = Page.of(reqVo.getPageNum(),reqVo.getPageSize());
        Page<FinanceResVo> page = financeDeptMapper.findSunDeptPage(pageReq,reqVo);
        if(ObjectUtil.isNull(page) || CollUtil.isEmpty(page.getRecords())){
            return PageUtils.cover(reqVo);
        }
        return PageUtils.cover(page);
    }
    /**
     * 和上游部门对账
     * @param reqVo
     * @return
     */
    @Override
    public PageResult<FinanceResVo> findParentDeptPage(FinanceReqVo reqVo)
    {
        LoginUserVo userVo = SecurityUtils.getLoginUser();
        reqVo.setDeptId(userVo.getDeptId());
        IPage<?> pageReq = Page.of(reqVo.getPageNum(),reqVo.getPageSize());
        Page<FinanceResVo> page = financeDeptMapper.findParentDeptPage(pageReq,reqVo);
        if(ObjectUtil.isNull(page) || CollUtil.isEmpty(page.getRecords())){
            return PageUtils.cover(reqVo);
        }
        page.getRecords().forEach(p->{
            p.setDeptName(userVo.getDeptName());
        });
        return PageUtils.cover(page);
    }

    /**
     * 查询统计的数据
     * @param reqVo
     * @return
     */
    @Override
    public List<FinanceStatsResVo> findDeptStaticPage(FinanceStatsReqVo reqVo)
    {
        FinanceDept financeDept = financeDeptMapper.selectById(reqVo.getFinanceId());
        FinanceDeptBatch deptBatch = financeDeptBatchMapper.selectById(financeDept.getBatchId());
        reqVo.setDeptId(financeDept.getDeptId());
        reqVo.setStatDay(DateUtil.formatDate(deptBatch.getStartTime()));
        reqVo.setEndDay(DateUtil.formatDate(DateUtil.offsetDay(deptBatch.getEndTime(),-1)));
        return financeStaticMapper.findDeptStaticList(reqVo);

    }
    /**
     * 提交财务信息
     * @param reqVo
     */
    @Override
    public void submitFinance(SubmitFinanceVo reqVo)
    {
        FinanceDept financeDept = financeDeptMapper.selectById(reqVo.getId());
        if(ObjectUtil.isNull(financeDept)){
            throw new ModularException(BaseCodeDesc.DEFATUL_MSG,"财务数据错误");
        }
        if(ObjectUtil.isNotNull(financeDept.getSalaryType()) && ObjectUtil.notEqual(financeDept.getSalaryType(), RecordConstant.SalaryType.NEW_RECORD)){
            throw new ModularException(BaseCodeDesc.DEFATUL_MSG,"已经提交，不能重复提交");
        }
        LambdaUpdateWrapper<FinanceDept> modify = Wrappers.lambdaUpdate();
        modify.set(FinanceDept::getSubmitFinanceType,reqVo.getSubmitFinanceType());
        modify.set(FinanceDept::getSubmitRemark,reqVo.getSubmitRemark());
        modify.set(FinanceDept::getSalaryType, RecordConstant.SalaryType.SEND_SALARY);
        modify.set(FinanceDept::getSubmitDate, DateUtil.date());
        modify.set(FinanceDept::getModifyDate,DateUtil.date()).set(FinanceDept::getModifyUserId,SecurityUtils.getUserId());
        modify.eq(FinanceDept::getId,financeDept.getId());
        financeDeptMapper.update(modify);
    }

    /**
     * 提交财务信息
     * @param reqVo
     */
    @Override
    public void editFinance(SubmitFinanceVo reqVo)
    {
        FinanceDept financeDept = financeDeptMapper.selectById(reqVo.getId());
        if(ObjectUtil.isNull(financeDept)){
            throw new ModularException(BaseCodeDesc.DEFATUL_MSG,"财务数据错误");
        }
        if( ObjectUtil.equal(financeDept.getSalaryType(), RecordConstant.SalaryType.CONFIRM_SALARY)){
            throw new ModularException(BaseCodeDesc.DEFATUL_MSG,"已经核查，不能修改");
        }
        LambdaUpdateWrapper<FinanceDept> modify = Wrappers.lambdaUpdate();
        modify.set(FinanceDept::getSubmitFinanceType,reqVo.getSubmitFinanceType());
        modify.set(FinanceDept::getSubmitRemark,reqVo.getSubmitRemark());
        modify.set(FinanceDept::getModifyDate,DateUtil.date()).set(FinanceDept::getModifyUserId,SecurityUtils.getUserId());
        modify.eq(FinanceDept::getId,financeDept.getId());
        financeDeptMapper.update(modify);
    }

    /**
     * 提交财务信息
     * @param id
     */
    @Override
    public void checkFinance(String id)
    {
        FinanceDept financeDept = financeDeptMapper.selectById(id);
        if(ObjectUtil.isNull(financeDept)){
            throw new ModularException(BaseCodeDesc.DEFATUL_MSG,"财务数据错误");
        }
        if(ObjectUtil.isNotNull(financeDept.getSalaryType()) && ObjectUtil.notEqual(financeDept.getSalaryType(), RecordConstant.SalaryType.SEND_SALARY)){
            throw new ModularException(BaseCodeDesc.DEFATUL_MSG,"未提交财务对账，不能核查");
        }
        LambdaUpdateWrapper<FinanceDept> modify = Wrappers.lambdaUpdate();
        modify.set(FinanceDept::getSalaryType, RecordConstant.SalaryType.CONFIRM_SALARY);
        modify.set(FinanceDept::getModifyDate,DateUtil.date()).set(FinanceDept::getModifyUserId,SecurityUtils.getUserId());
        modify.eq(FinanceDept::getId,financeDept.getId());
        financeDeptMapper.update(modify);
    }
}
